package com.mzy.interview

/**
 * @Author: mizy3
 * @Date: 2024/08/25/下午7:52
 * @Description:
 */
class 旋转链表 {
    fun rotateRight(head: ListNode?, k: Int): ListNode? {
        if (head?.next == null) return head;
        var cur: ListNode? = head
        var len = 0
        var tail: ListNode? = null
        while (cur != null) {
            tail = cur
            cur = cur.next
            len++
        }
        var n = k % len;
        if(n == 0) return head
        cur = head
        var p  = len - n - 1
        while (n > 0) {
            cur = cur?.next
            n--
        }

        val newHead = cur?.next

        cur?.next = null
        tail?.next = head;

        return newHead
    }
}

fun main() {
    旋转链表().rotateRight(ListNode(0, ListNode(1, ListNode(2))), 4)
}